import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/double_control_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/risk_control_page/risk_security_identification_page/risk_security_identification_controller.dart';

///安全风险辨识
class RiskSecurityIdentificationPage extends StatelessWidget {
  RiskSecurityIdentificationPage({super.key}){
    Get.lazyPut(RiskSecurityIdentificationController.new);
  }
  @override
  Widget build(BuildContext context) {
    final RiskSecurityIdentificationController controller = Get.find<RiskSecurityIdentificationController>();
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          key: controller.riskRightScaffoldKey,
          body: Stack(
            children: [
              Container(
                color: AppColors.bg_color_grey,
                padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 14.w),
                child: Column(
                  children: [
                    Row(
                      children: [
                        Expanded(child: _getBox(controller)),
                      ],
                    ),
                    AppGaps.vGap14,
                    Expanded(
                      child: CustomEasyRefreshList<DoubleControlChildren>(
                        key: controller.globalKey,
                        dataLoad: controller.getRiskSecurityIdentificationList,
                        enableLoad: false,
                        itemBuilder: (item, index) {
                          return CustomTitleChartPage(
                            titleRightWidget: _getResult(item,controller),
                            title: '${_stringWho(item)}${item.name ?? ''}',
                            child: _levelShow(item,controller),
                          ).click(() {
                            //level:1 全部 2 对象 3 单元 4 事件 5 措施
                            if (item.level == 1) {
                              //全部
                              // controller.onRiskButtonObjPressed();
                            } else if (item.level == 2) {
                              //对象
                              controller.onRiskButtonObjPressed(item.id);
                            } else if (item.level == 3) {
                              //单元
                              controller.onRiskButtonUnitPressed(item.id);
                            } else if (item.level == 4) {
                              //事件
                              controller.onRiskButtonEventPressed(item.id);
                            } else if (item.level == 5) {
                              //管控措施
                              controller.onRiskButtonStepPressed(item.id);
                            }
                          });
                        },
                      ),
                    )
                  ],
                ),
              ),
              Positioned(
                  right: 27,
                  bottom: 160,
                  child: SvgPicture.asset(
                    AppImages.icon_all_add_data,
                    width: 50.w,
                    height: 50.w,
                  ).clickOnce(() {
                    if (controller.currentPageLevel == 1) {
                      //全部
                    } else if (controller.currentPageLevel == 2) {
                      //对象
                      controller.onClickAddObj();
                    } else if (controller.currentPageLevel == 3) {
                      //单元
                      controller.onClickAddUnit();
                    } else if (controller.currentPageLevel == 4) {
                      //事件
                      controller.onClickAddEvent();
                    } else if (controller.currentPageLevel == 5) {
                      //管控措施
                      controller.onClickAddStep();
                    }
                  })),
            ],
          ),
          // endDrawer: CustomEndDrawer(
          //   centerContext: Column(
          //     children: [
          //       RegionalOrganizationTreePage(
          //         selectedEntity: controller.selectedEntity,
          //         onCallBack: (RiskTreeEntity entity) {
          //           controller.selectedEntity = entity;
          //         },
          //       ),
          //
          //     ],
          //   ),
          //   reset: controller.reset,
          //   confirm: controller.confirm,
          // ),
        );
      },
    );
  }

  Widget _title(String title, String? value) {
    return Row(
      children: [
        Text(
          '$title：',
          style: TextStyle(
            fontSize: 13.sp,
            fontWeight: FontWeight.w400,
            color: const Color(0xffA5A6B5),
          ),
        ),
        Expanded(
          child: Align(
            alignment: Alignment.centerRight,
            child: Text(
              value ?? '空',
              style: TextStyle(
                fontSize: 12.sp,
                fontWeight: FontWeight.w400,
                color: const Color(0xff495061),
              ),
            ),
          ),
        ),
      ],
    );
  }

  Widget _getBox(RiskSecurityIdentificationController controller) {
    return Container(
      height: 69.w,
      padding: EdgeInsets.all(12.w),
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(5)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Expanded(
            child: Text(
              '${controller.title}',
              style: TextStyle(
                color: const Color(
                  0xFF495061,
                ),
                fontWeight: FontWeight.w500,
                fontSize: 12.sp,
              ),
            ),
          ),
          Row(
            children: [
              _divider(),
              AppGaps.hGap8,
              Container(
                width: 38.w,
                height: 38.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
                child: SvgPicture.asset(
                  AppImages.icon_search_screen,
                  width: 16.w,
                ),
              ),
            ],
          ),
        ],
      ),
    ).click(controller.onFilterButtonPressed);
  }

  ///分割线
  Widget _divider() {
    return Container(
      width: 1.w,
      height: 40.w,
      decoration: const BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
        Color(0x00649FFF),
        Color(0xFF649FFF),
        Color(0x00649FFF),
      ])),
    );
  }

  Widget _getResult(DoubleControlChildren item, RiskSecurityIdentificationController controller) {
    return Row(
      children: [
        Text(
          '更多',
          style: TextStyle(
            fontSize: 12.sp,
            fontWeight: FontWeight.w400,
            color: const Color(0xff5C6DFF),
          ),
        ),
        Container(
          margin: EdgeInsets.only(left: 4.w),
          child: SvgPicture.asset(
            AppImages.icon_new_see_details_right,
            width: 14.w,
            height: 14.w,
          ),
        ),
      ],
    ).click(() {
      controller.selectQualityAndLevel(item);
    });
  }

  String _stringWho(DoubleControlChildren item) {
    //level:1 全部 2 对象 3 单元 4 事件 5 措施
    if (item.level == 1) {
      return '全部：';
    } else if (item.level == 2) {
      return '对象：';
    } else if (item.level == 3) {
      return '单元：';
    } else if (item.level == 4) {
      return '事件：';
    } else if (item.level == 5) {
      return '管控措施：';
    }
    return '全部：';
  }

  Widget _levelShow(DoubleControlChildren item, RiskSecurityIdentificationController controller) {
    if (item.level == 1) {
      //全部
    } else if (item.level == 2) {
      //对象
      return Column(
        children: [
          _title('责任人', item.hazardLiablePerson ?? ''),
          AppGaps.vGap12,
          _title('责任部门', item.hazardDep ?? ''),
        ],
      );
    } else if (item.level == 3) {
      //单元
      return Column(
        children: [
          AppGaps.vGap12,
          _title('辨识部门', item.hazardDistinguishDep ?? ''),
          AppGaps.vGap12,
          _title('辨识人', item.hazardDistinguishPerson ?? ''),
        ],
      );
    } else if (item.level == 4) {
      //事件
      return Column(
        children: [
          _title('初始风险等级', controller.backRiskLevelName(item.initialRiskLevel)),
        ],
      );
    } else if (item.level == 5) {
      //管控措施
      return Column(
        children: [
          _title('管控手段', item.dataSrc ?? ''),
        ],
      );
    }
    return Column(
      children: [
        _title('责任人', item.hazardLiablePerson ?? ''),
        AppGaps.vGap12,
        _title('责任部门', item.hazardDep ?? ''),
        AppGaps.vGap12,
        _title('管控手段', item.dataSrc ?? ''),
      ],
    );
  }

}
